<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AppointViewRule;
use App\Admin\Repositories\Hospital;
use App\Models\HospitalAppointType;
use App\Models\Vaccine;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AppointViewRuleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AppointViewRule(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('type');
            $grid->column('value');
            $grid->column('weight');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->disableFilterButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AppointViewRule(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('value');
            $show->field('weight');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AppointViewRule(), function (Form $form) {
            $rule_id = request()->input('rule_id');
            $weight = request()->input('weight');
            $form->hidden('value');
            $form->hidden('id');
            $form->select('type')
                ->when(1,function (Form $form)  {
                    $form->select('values.value1','预约项目')
                        ->options(HospitalAppointType::query()->pluck('name','id'))
                        ->customFormat(function ($v) {
                            return $this->value;
                        });
                    ;
                })
                ->when(2,function (Form $form)  {
                    $form->select('values.value2','社区医院')
                        ->options(\App\Models\Hospital::query()->pluck('name','id'))
                        ->customFormat(function ($v) {
                            return $this->value;
                        });
                })
                ->when(3,function (Form $form)  {
                    $form->text('values.value3','出生日期开始（大于x天）')->customFormat(function ($v) {
                        return $this->value;
                    });
                })
                ->when(4,function (Form $form)  {
                    $form->text('values.value4','出生日期结束（小于x天）')->customFormat(function ($v) {
                        return $this->value;
                    });
                })
                ->when(5,function (Form $form)  {
                    $form->date('values.value5','出生日期开始（日期）')->customFormat(function ($v) {
                        return date('Y-m-d',$this->value);
                    });
                })
                ->when(6,function (Form $form)  {
                    $form->date('values.value6','出生日期结束（日期）')->customFormat(function ($v) {
                        return date('Y-m-d',$this->value);
                    });
                })
                ->when(7,function (Form $form)  {
                    $form->select('values.value7','疫苗')->options(Vaccine::query()->pluck('name','id'))->customFormat(function ($v) {
                        return $this->value;
                    });
                })
                ->options(\App\Models\AppointViewRule::$typeText)
                ->required();
            $form->hidden('weight')->value($weight);
            $form->hidden('rule_id')->value($rule_id);
            $form->submitted(function (Form $form) {

                $types = \App\Models\AppointViewRule::query()->where('rule_id',$form->rule_id)->pluck('type')->toArray();

                if($types){
                    if(in_array($form->type,[3,4]) && (in_array(5,$types) || in_array(6,$types))){
                        $form->responseValidationMessages('type', '已设置了出生日期，出生天数不可设置');
                    }
                    if(in_array($form->type,[5,6]) && (in_array(3,$types) || in_array(4,$types))){
                        $form->responseValidationMessages('type', '已设置了出生天数，出生日期不可设置');
                    }

                }


                // 获取用户提交参数
                $value = $form->values;

                $firstNonNullValue = current(array_filter($value));
                $form->value = $firstNonNullValue;
                $form->deleteInput('values');

                if(in_array($form->type,[5,6])){
                    $form->value = strtotime($form->value);
                }

                if(in_array($form->type,[3,4,5,6])){
                    $appointViewRule = \App\Models\AppointViewRule::query()
                        ->where('rule_id', $form->rule_id)
                        ->where('type', $form->type)
                        ->where('id','!=',$form->id)
                        //->where('value', $form->value)
                        ->first();
                }else {
                    $appointViewRule = \App\Models\AppointViewRule::query()
                        ->where('rule_id', $form->rule_id)
                        ->where('type', $form->type)
                        ->where('value', $form->value)
                        ->where('id','!=',$form->id)
                        ->first();
                }


                if($appointViewRule){
                    $form->responseValidationMessages('type', '当前规则已设置');
                }


            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
